package com.sun.filesigndemo.controller;

import com.github.pagehelper.PageHelper;
import com.sun.filesigndemo.bussiness.mapper.UserMapper;
import com.sun.filesigndemo.common.entity.User;
import com.sun.filesigndemo.common.pagehelper.MyPageInfo;
import com.sun.filesigndemo.common.security.UserUtils;
import com.sun.filesigndemo.common.statusCode.ResultMsg;
import com.sun.filesigndemo.common.utils.StringUtils;
import org.apache.http.HttpRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("user")
public class UserController extends BaseController {


	@Autowired
    UserMapper userMapper;

//    @RequiresPermissions("admin")
	@RequestMapping(value="list", method= RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> index(HttpServletRequest request, User user) {
        PageHelper.startPage(user.getPage(), user.getSize());
        List list = userMapper.findAllUserAndRole();
        //MyPageInfo<> userList = new MyPageInfo()
        MyPageInfo<User> userList= new MyPageInfo(list);
	    return ResultMsg.successData(userList);
	}

	@RequestMapping(value = "nameList",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getNameList(HttpRequest request){
	    List list = userMapper.getUserNameList();
	    return ResultMsg.successData(list);
    }



//    @RequiresPermissions("admin")
	@RequestMapping(method= RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> index2(HttpServletRequest request, User user, String newPwd, String rolesId, String isUpdate) {
	    int a = 1;
	    String[] str = rolesId.split(",");
        if (user.getId() != null) {
            User oldUser = userMapper.selectByPrimaryKey(user.getId());

            if (!oldUser.getName().equals(user.getName())) {

                Example example = new Example(User.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("name", user.getName());
                if (userMapper.selectCountByExample(example) > 0)
                    return ResultMsg.fail("用户名已存在");
            }

            if (!oldUser.getPhone().equals(user.getPhone())) {

                Example example = new Example(User.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("phone", user.getPhone());
                if (userMapper.selectCountByExample(example) > 0)
                    return ResultMsg.fail("手机已存在");
            }
            if (StringUtils.isNotEmpty(newPwd)) {
                user.setPwd(UserUtils.entryptPassword(newPwd));
            }


            user.setCreateTime(null);
            user.setUpdateTime(new Date());
            userMapper.updateByPrimaryKeySelective(user);
            userMapper.deleteUsersAllRole(user.getId());
            for(int i = 0;i<str.length;i++){
                userMapper.insertRoleToUser(user.getId(),Integer.parseInt(str[i]));
            }
            //userMapper.updateRoleToUser(user.getId(),Integer.valueOf(7));
            return ResultMsg.successData(user);
        }

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name", user.getName());
        if (userMapper.selectCountByExample(example) > 0)
            return ResultMsg.fail("用户名已存在");

        Example example2 = new Example(User.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("phone", user.getPhone());
        if (userMapper.selectCountByExample(example2) > 0)
            return ResultMsg.fail("手机已存在");

        if (StringUtils.isEmpty(newPwd)) {
            return ResultMsg.fail("密码不能为空");
        }

        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setPwd(UserUtils.entryptPassword(newPwd));

        int n = 0;
        userMapper.insertUseGeneratedKeys(user);
        for(int i = 0;i<str.length;i++){
           //n = userMapper.updateRoleToUser(user.getId(),Integer.parseInt(str[i]));
            n = userMapper.insertRoleToUser(user.getId(),Integer.parseInt(str[i]));
        }
        //int m = userMapper.insertRoleToUser(user.getId(),user.getRid());
        return ResultMsg.successData(user);
    }
}
